﻿package com.wly.ui.panel
{
	import com.wly.datapool.DataPool;
	import com.wly.event.EventCodes;
	import com.wly.event.EventManager;
	import com.wly.ui.scene.SceneDefine;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class ResultPanel extends Sprite
	{
		private var _tfCurScore:TextField;
		private var _tfMaxScore:TextField;
		private var _btnReplay:SimpleButton;
		private var _btnBack:SimpleButton;
		
		public function ResultPanel()
		{
			super();
			initChilds();
		}
		
		private function initChilds():void
		{
			_tfCurScore = getChildByName("tfCurScore") as TextField;
			_tfMaxScore = getChildByName("tfMaxScore") as TextField;
			_btnReplay  = getChildByName("btnReplay") as SimpleButton;
			_btnReplay.addEventListener(MouseEvent.CLICK, replay);
			
			_btnBack = getChildByName("btnBack") as SimpleButton;
			_btnBack.addEventListener(MouseEvent.CLICK, onBack);
			
			setMaxScore(DataPool.getInstance().mainPlayer.maxScore);
			EventManager.getInstance().registerEvent(EventCodes.EVENT_PLAYER_MAXSCORE_UPDATE, onMaxScoreUpdate);
		}
		
		private function onMaxScoreUpdate(e:*):void
		{
			var newScore:int = e as int;
			setMaxScore(newScore);
		}
		
		public function setCurScore(val:int):void
		{
			if(_tfCurScore)
			{
				_tfCurScore.text = val.toString();
			}
		}
		
		public function setMaxScore(val:int):void
		{
			if(_tfMaxScore)
			{
				_tfMaxScore.text = val.toString();
			}
		}
		
		private function replay(e:MouseEvent):void
		{
			trace(this, "replay");
			EventManager.getInstance().dispatchEvent(EventCodes.EVENT_SCENE_UPDATE, SceneDefine.SCENE_GAME);
		}
		
		private function onBack(e:MouseEvent):void
		{
			trace(this, "onBack");
			EventManager.getInstance().dispatchEvent(EventCodes.EVENT_SCENE_UPDATE, SceneDefine.SCENE_WELCOME);
		}
	}
}